package pit.com.ordermobile.adapter;

import pit.com.ordermobile.classer.Category;
import pit.com.ordermobile.classer.Item;
import pit.com.ordermobile.modum.ChildViewHolder;
import pit.com.ordermobile.modum.ParentViewHolder;
import pit.com.ordermobilesystem.OrderMobileProvider;
import pit.com.ordermobilesystem.OrderMobileProvider.CategoryTable;
import pit.com.ordermobilesystem.OrderMobileProvider.ItemTable;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorTreeAdapter;

public class OrderMobileAdapter extends CursorTreeAdapter {

	LayoutInflater mInflator;
	Context mcontext;
	Cursor mcursor;

	public OrderMobileAdapter(Cursor cursor, Context context) {
		super(cursor, context);
		// TODO Auto-generated constructor stub
		//mInflator = LayoutInflater.from(context);
		mcontext = context;
		mcursor = cursor;

	}

	@Override
	protected void bindChildView(View view, Context context, Cursor cursor,boolean isLastChild) {
		// TODO Auto-generated method stub
		Item item = new Item();
		OrderMobileProvider.getItem(cursor, item);
		// int IdItem = item.getId() ;
		String nameitem = item.getNameItem();
		String description = item.getDescription();
		// cho nay lay Img lam sao ta
		// String linking = item.getLinkImage();
		ChildViewHolder child = new ChildViewHolder(context);
		if (view == null) {
			child.tv_nameitem.setText(nameitem);
			child.tv_description.setText(description);
			// child.img_dish.set
		}

	}

	@Override
	protected void bindGroupView(View view, Context context, Cursor cursor,boolean isExpanded) {
		// TODO Auto-generated method stub
		Category category = new Category();
		OrderMobileProvider.getCategory(cursor, category);
		String NameCategory = category.getNameCategory();
		ParentViewHolder group = new ParentViewHolder(context);
		if (view == null) {
			group.tv_category.setText(NameCategory);
		}
	}

	@Override
	protected Cursor getChildrenCursor(Cursor groupCursor) {
		// TODO Auto-generated method stub
		OrderMobileProvider provider = new OrderMobileProvider();
		// Uri.Builder builder = provider.ITEM_CONTENT_URI.buildUpon();
		// không biết giải quyết sao nữa
		Cursor cur = provider.query(provider.ITEM_CONTENT_URI,new String[] { ItemTable.ID_ITEM,
				ItemTable.ID_CATEGORY, ItemTable.NAME_ITEM,ItemTable.PRICE, ItemTable.DESCRIPTION,
				ItemTable.LINK_IMAGE, ItemTable.DISCOUNT },ItemTable.ID_CATEGORY
				+ "=="+ groupCursor.getInt(groupCursor.getColumnIndexOrThrow(CategoryTable.ID_CATEGORY)),
				null, CategoryTable.NAME_CATEGORY);
		/*
		 * return managedQuery(builder.build(), new String[] {
		 * ItemTable.ID_ITEM, ItemTable.ID_CATEGORY ,
		 * ItemTable.NAME_ITEM,ItemTable.PRICE,
		 * ItemTable.DESCRIPTION,ItemTable.LINK_IMAGE,ItemTable.DISCOUNT},
		 * ItemTable.ID_CATEGORY + "=="+
		 * groupCursor.getInt(groupCursor.getColumnIndexOrThrow
		 * (CategoryTable.ID_CATEGORY)), null,CategoryTable.NAME_CATEGORY);
		 */
		return cur;
	}

	@Override
	protected View newChildView(Context context, Cursor cursor,
			boolean isLastChild, ViewGroup parent) {
		// TODO Auto-generated method stub
		final LayoutInflater inflater = LayoutInflater.from(context);
		View v = (ViewGroup) inflater.inflate(android.R.layout.activity_list_item, parent, false);
		return v;
	}

	@Override
	protected View newGroupView(Context context, Cursor cursor,
			boolean isExpanded, ViewGroup parent) {
		// TODO Auto-generated method stub
		final LayoutInflater inflater = LayoutInflater.from(context);
		View v = (ViewGroup) inflater.inflate(android.R.layout.activity_list_item, parent, false);
		return v;
	}

}
